<template>
    <div>
        <a-modal
            class="server-editing-model-v"
            title="在线编辑"
            :visible="serverEditingVisible"
            @cancel="serverEditingVisible = false"
            @handleOk="serverEditingVisible = false"
            :forceRender="true"
            width="100%"
            wrapClassName="full-modal"
        >
            <a-spin size="large" :spinning="serverEditingSpinning">
                <div style="margin-bottom: 15px;">
                    <a-button style="margin-right: 15px;" class="blue-btn" type="primary" @click="refreshClick">刷新</a-button>
                    <a-button v-if="isPdf && rowObj.type !== '质量体系文件'" style="margin-right: 15px;" class="blue-btn" type="primary" @click="reEditClick">重新编辑</a-button>
                    <a-button v-else style="margin-right: 15px;" class="blue-btn" type="primary" @click="saveClick">保存</a-button>
                    <a-button style="margin-right: 15px;" class="blue-btn" type="primary" @click="closeClick">关闭</a-button>
                    <a-button style="margin-right: 15px;" v-if="!isPdf && !rowObj.isTemplate && rowObj.type !== '质量体系文件'" class="blue-btn" type="primary" @click="generatePDFClick">生成PDF</a-button>
                    <a-button style="margin-right: 15px;" v-if="isPdf && !rowObj.isTemplate && rowObj.type !== '质量体系文件'&&!rowObj.isGz" class="blue-btn" type="primary" @click="stampClick">盖章</a-button>
                    <a-button v-if="!isPdf && !rowObj.isTemplate && rowObj.type !== '质量体系文件'&&!rowObj.isGz" class="blue-btn" type="primary" @click="generatePDFStampClick">生成PDF并盖章</a-button>
                </div>
                <div :key="iframeDataKey">
                    <iframe 
                        v-if="iframeSrc"
                        :key="iframeKey"
                        :src="iframeSrc" 
                        ref="iframeServerEditingRef" 
                        scrolling="auto" 
                        width="100%" 
                        :height="(windowInnerHeight -170 )+'px'"
                        style="border: 0;"
                    ></iframe>
                    <span v-else>当前文件发生错误，请刷新！</span>
                </div>
                
            </a-spin>
            <template #footer></template>
        </a-modal>
    </div>
</template>
<script>
import {
    getWebOfficeDownloadFile,
    wjmbWebOfficeApiGetWebOfficeDownloadFile,
    openFile,
    getDocxToPdf,
    againEditPdfOnlineFile,
    getDocxToPdfAndGz,
    pdfFileHcCa,
    wjmbWebOfficeApiOpenFile,
    ZltxwjWebOfficeApiOpenFile,
    ZltxwjWebOfficeApiGetWebOfficeDownloadFile,
} from '@/api/manage.js'
import YchConstant from '@/api/YchConstant'
export default {
    components:{

    },
    props:{

    },
    data(){
        return{
            iframeSrc:'',
            serverEditingVisible:false,
            isPdf:false,
            rowObj:{},
            iframeKey:null,
            iframeDataKey:null,
            serverEditingSpinning:false,
            windowInnerHeight:window.innerHeight,
        }
    },
    watch:{

    },
    mounted(){
        
    },
    methods:{
        refreshClick(){
            this.serverEditingSpinning = true
            this.iframeDataKey = Date.now()
            if(this.rowObj.fwjlx == '.doc' || this.rowObj.fwjlx == '.docx'){
                this.isPdf = false
                this.openFileClick(this.rowObj)
                
            }else{
                this.isPdf = true
                this.cancelLoading()
                if(this.rowObj.type == '质量体系文件'){
                    this.iframeSrc = bootConfig[process.env.NODE_ENV].locality_ApI + YchConstant.SPZX_SPCY + '/flow-zltxwj/getFileCross?id='+this.rowObj.id
                }else{
                    this.iframeSrc = bootConfig[process.env.NODE_ENV].locality_ApI + YchConstant.SPZX_SPCY + '/spcyRsWjmb/openWjjl?id='+this.rowObj.id

                }
            }
        },
        cancelLoading(){
            let timer = setInterval((val)=>{
                this.serverEditingSpinning = false
                clearInterval(timer);
            },500)
            
        },
        getData(val){//获取传参
            this.serverEditingVisible = true
            this.serverEditingSpinning = true
            this.rowObj = {...val,isTemplate:val.isTemplate?true:false}
            if(val.fwjlx == '.doc' || val.fwjlx == '.docx'){
                this.isPdf = false
                this.openFileClick(this.rowObj)
            }else{
                this.isPdf = true
                this.cancelLoading()
                if(val.type == '质量体系文件'){
                    this.iframeSrc = bootConfig[process.env.NODE_ENV].locality_ApI + YchConstant.SPZX_SPCY + '/flow-zltxwj/getFileCross?id='+this.rowObj.id
                }else{
                    this.iframeSrc = bootConfig[process.env.NODE_ENV].locality_ApI + YchConstant.SPZX_SPCY + '/spcyRsWjmb/openWjjl?id='+this.rowObj.id

                }
                
            }
            
        },
        openFileClick(val){
            this.serverEditingSpinning = true
            if(this.rowObj.isTemplate){
                wjmbWebOfficeApiOpenFile({id:val.id}).then(res=>{
                    if(res.code == 200){
                        this.$emit('refreshChange')
                        this.iframeSrc = res.result.wordUrl
                        this.iframeKey = Date.now()
                    }else{
                        this.$message.warning(res.message)
                    }
                    this.cancelLoading()
                })
            }else{
                if(val.type == '质量体系文件'){
                    ZltxwjWebOfficeApiOpenFile({id:val.id}).then(res=>{
                        if(res.code == 200){
                            //console.log(res)
                            this.$emit('refreshChange')
                            this.iframeSrc = res.result.wordUrl
                            this.iframeKey = Date.now()
                        }else{
                            this.$message.warning(res.message)
                        }
                        this.cancelLoading()
                    })
                }else{
                    openFile({id:val.id}).then(res=>{
                        if(res.code == 200){
                            //console.log('重新编辑',res)
                            this.$emit('refreshChange')
                            this.iframeSrc = res.result.wordUrl
                            this.iframeKey = Date.now()
                        }else{
                            this.$message.warning(res.message)
                        }
                        this.cancelLoading()
                    })
                }
                
            }
            
        },
        reEditClick(){
            this.serverEditingSpinning = true
            againEditPdfOnlineFile({id:this.rowObj.id}).then(res=>{
                if(res.code == 200){
                    this.$emit('refreshChange')
                    this.isPdf = false
                    this.openFileClick(this.rowObj)
                    this.iframeKey = Date.now()
                }else{
                    this.$message.warning(res.message)
                }
                this.cancelLoading()
            })
        },
        saveClick(){
            this.serverEditingSpinning = true
            if(this.rowObj.isTemplate){
                wjmbWebOfficeApiGetWebOfficeDownloadFile({id:this.rowObj.id}).then(res=>{
                    if(res.code == 200){
                        this.$emit('refreshChange')
                        this.openFileClick(this.rowObj)
                        this.$message.success(res.message)
                    }else{
                        this.$message.warning(res.message)
                    }
                    this.cancelLoading()
                })
            }else{
                if(this.rowObj.type == '质量体系文件'){
                    ZltxwjWebOfficeApiGetWebOfficeDownloadFile({id:this.rowObj.id}).then(res=>{
                        if(res.code == 200){
                            this.$emit('refreshChange')
                            this.openFileClick(this.rowObj)
                            this.$message.success(res.message)
                        }else{
                            this.$message.warning(res.message)
                        }
                        this.cancelLoading()
                    })
                }else{
                    getWebOfficeDownloadFile({id:this.rowObj.id}).then(res=>{
                        if(res.code == 200){
                            this.$emit('refreshChange')
                            this.openFileClick(this.rowObj)
                            this.$message.success(res.message)
                        }else{
                            this.$message.warning(res.message)
                        }
                        this.cancelLoading()
                    })
                }
                
            }
            
        },
        closeClick(){
            if(this.rowObj.fwjlx == '.doc' ||this.rowObj.fwjlx == '.docx'){
                if(this.rowObj.isTemplate){
                    wjmbWebOfficeApiGetWebOfficeDownloadFile({id:this.rowObj.id,isClose:true}).then(res=>{
                        if(res.code == 200){
                            this.$emit('refreshChange')
                            this.serverEditingVisible = false
                            this.$message.success(res.message)
                        }else{
                            this.$message.warning(res.message)
                        }
                    })
                }else{
                    if(this.rowObj.type == '质量体系文件'){
                        ZltxwjWebOfficeApiGetWebOfficeDownloadFile({id:this.rowObj.id,isClose:true}).then(res=>{
                            if(res.code == 200){
                                this.$emit('refreshChange')
                                this.serverEditingVisible = false
                                this.$message.success(res.message)
                            }else{
                                this.$message.warning(res.message)
                            }
                        })
                    }else{
                        getWebOfficeDownloadFile({id:this.rowObj.id,isClose:true}).then(res=>{
                            if(res.code == 200){
                                this.$emit('refreshChange')
                                this.serverEditingVisible = false
                                this.$message.success(res.message)
                            }else{
                                this.$message.warning(res.message)
                            }
                        })
                    }
                }
            }else{
                this.serverEditingVisible = false
            }
            
        },
        generatePDFClick(){
            this.serverEditingSpinning = true
            getDocxToPdf({id:this.rowObj.id}).then(res=>{
                if(res.code == 200){
                    this.$emit('refreshChange')
                    this.isPdf = true
                    this.rowObj.fwjlx = '.pdf'
                    this.iframeSrc = bootConfig[process.env.NODE_ENV].locality_ApI + YchConstant.SPZX_SPCY + '/spcyRsWjmb/openWjjl?id='+this.rowObj.id
                    this.iframeKey = Date.now()
                }else{
                    this.$message.warning(res.message)
                }
                this.cancelLoading()
            })
            // `api/spzx-spcy/spcyRsWjmb/openWjjl?id=${res.result.id}`
        },
        //盖章
        stampClick(){
            pdfFileHcCa({id:this.rowObj.id}).then(res=>{
                if(res.code == 200){
                    this.$emit('refreshChange')
                    this.isPdf = true
                    this.rowObj.fwjlx = '.pdf'
                    this.iframeSrc = bootConfig[process.env.NODE_ENV].locality_ApI + YchConstant.SPZX_SPCY + '/spcyRsWjmb/openWjjl?id='+this.rowObj.id
                    this.iframeKey = Date.now()
                }else{
                    this.$message.warning(res.message)
                }
            })
            
        },
        //保存成pdf并且盖章
        generatePDFStampClick(){
            getDocxToPdfAndGz({id:this.rowObj.id}).then(res=>{
                if(res.code == 200){
                    this.$emit('refreshChange')
                    this.isPdf = true
                    this.rowObj.fwjlx = '.pdf'
                    this.iframeSrc = bootConfig[process.env.NODE_ENV].locality_ApI + YchConstant.SPZX_SPCY + '/spcyRsWjmb/openWjjl?id='+this.rowObj.id
                    this.iframeKey = Date.now()
                }else{
                    this.$message.warning(res.message)
                }
            })
        },
    }
}
</script>
<style lang="less">

.full-modal {
  .ant-modal {
    max-width: 100%;
    top: 0;
    padding-bottom: 0;
    margin: 0;
  }
  .ant-modal-content {
    display: flex;
    flex-direction: column;
    height: calc(100vh);
  }
  
}
.server-editing-model-v{
    .ant-modal-body {
        flex: 1;
        background: #fff !important;
    }
    .ant-modal-footer{
        background: #fff !important;
    }
}

</style>